package com.yeqian.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yeqian.entity.PageResult;
import com.yeqian.entity.QueryPageBean;
import com.yeqian.entity.Result;
import com.yeqian.pojo.Article;
import com.yeqian.pojo.Tag;
import com.yeqian.service.ArticleService;
import com.yeqian.service.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author : yeqian
 * @Date : 2020/12/24 22:57
 * 文章服务实现类
 */
@Service
@Transactional
public class ArticleServiceImpl extends BaseService implements ArticleService {


    /**
     * 修改置顶的文章
     *
     * @param
     * @return void
     * @author yeqian
     * @date 2021/1/22 12:27
     */
    @Override
    public void updateArticleAndRealaTionShip(Integer articleId) {
        // 修改原来的文章为不置顶
        articleMapper.updateTop();
        // 修改传入的参数的文章为置顶
        articleMapper.updateArticleAndRealaTionShip(articleId);
        System.out.println("被指定的文章"+articleId);

    }

    /**
     * 设置是否置顶
     *
     * @param
     * @return void
     * @author yeqian
     * @date 2021/1/22 11:23
     */
    @Override
    public void saveArticleAndRealaTionShip(Map<String, Integer> map) {
        articleMapper.saveArticleAndRealaTionShip(map);
    }

    /**
     * 查询每月的文章数量
     *
     * @param month
     * @return java.lang.Integer
     * @author yeqian
     * @date 2021/1/17 11:32
     */
    @Override
    public Integer getArticleCountByMonth(Date month) {
        String date = new SimpleDateFormat("yyyy-MM").format(month) + "-31";
        Integer memberCountAfterDate = articleMapper.getArticleCountByMonth(date);
        return memberCountAfterDate;
    }

    /**
     * 根据名称查询
     *
     * @param name
     * @return java.util.List<com.yeqian.pojo.Article>
     * @author yeqian
     * @date 2021/1/16 13:20
     */
    @Override
    public List<Article> getArticleByTagId(String name) {

        Integer tagId = tagMapper.getTagByName(name);
        List<Article> articleByTagId = articleMapper.getArticleByTagId(tagId);
        return articleByTagId;
    }

    /**
     * 查询最近几篇文章
     *
     * @param maxNumber
     * @return java.util.List<com.yeqian.pojo.Article>
     * @author yeqian
     * @date 2021/1/14 21:08
     */
    @Override
    public List<Article> getRecent(Integer maxNumber) {
        return articleMapper.getRecent(maxNumber);
    }

    /**
     * 查询阅读量最多的几篇文章
     *
     * @param maxNumber
     * @return
     */
    @Override
    public List<Article> listOrderByReadCount(Integer maxNumber) {
        return articleMapper.listOrderByReadCount(maxNumber);
    }

    /**
     * 查询所有阅读量
     *
     * @param
     * @return java.util.List<com.yeqian.pojo.Article>
     * @author yeqian
     * @date 2021/1/13 11:12
     */
    @Override
    public List<Article> getArticleReadCount() {
        return articleMapper.getArticleReadCount();
    }

    /**
     * 获取所有的article的图片的url
     *
     * @param
     * @return java.util.List<java.lang.String>
     * @author yeqian
     * @date 2021/1/10 14:01
     */
    @Override
    public List<String> getUrl() {
        List<String> urls = articleMapper.getUrl();
        List<String> urlList = new LinkedList<>();
        //截取成我们可以删除的url
        for (String url : urls) {
            String[] split = url.split("com/");
            urlList.add(split[1]);
        }
        return urlList;
    }

    /**
     * 增加阅读量
     *
     * @param articleId
     * @return void
     * @author yeqian
     * @date 2021/1/3 10:45
     */
    @Override
    public void addReadCount(Integer articleId) {
        //第一次应是从数据库中查，第二次应该是
        Map map = new HashMap();
        map.put("count", 1);
        map.put("articleId", articleId);
        articleMapper.saveReadCount(map);
    }

    /**
     * 保存文章阅读量
     *
     * @param map
     * @return void
     * @author yeqian
     * @date 2021/1/2 18:10
     */
    @Override
    public void saveReadCount(Map map) {
        articleMapper.saveReadCount(map);
    }

    /**
     * 获取所有文章的id
     *
     * @param
     * @return java.util.List<java.lang.Integer>
     * @author yeqian
     * @date 2021/1/2 18:07
     */
    @Override
    public List<Integer> getAllArticleId() {
        return articleMapper.getAllArticleId();
    }

    /**
     * 查询所有人的文章记录
     *
     * @param queryPageBean
     * @return com.yeqian.entity.PageResult
     * @author yeqian
     * @date 2020/12/28 22:22
     */
    @Override
    public PageResult getAllArticleByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        Page<Article> page = articleMapper.getAllArticleByPage(Integer.parseInt(queryString));
        long total = page.getTotal();
        List<Article> result = page.getResult();
        //设置作者名字
        for (Article article : result) {
            String authorName = userMapper.getArticleName(article.getAuthor());
            article.setAuthorName(authorName);
        }
        //设置标签
        for (Article article : result) {
            addTagName(article);
        }
        return new PageResult(total, result);
    }

    /**
     * 查询最近n条文章记录
     *
     * @param map
     * @return java.util.List<com.yeqian.pojo.Article>
     * @author yeqian
     * @date 2020/12/28 15:00
     */
    @Override
    public List<Article> getRecentArticle(Map map) {
        List<Article> recentArticle = articleMapper.getRecentArticle(map);
        for (Article article : recentArticle) {
            addTag(article);
            addTagName(article);
        }

        return recentArticle;
    }

    /**
     * 抽取公共代码添加标签
     *
     * @return void
     * @author yeqian
     * @date 2020/12/29 11:14
     */
    public void addTagName(Article article) {

        Integer articleId = article.getId();
        List<String> tagByArticle = tagMapper.getTagNameByArticle(articleId);
        article.setTagNames(tagByArticle);//设置文章的标签集

    }

    /**
     * 抽取公共代码添加标签
     *
     * @return void
     * @author yeqian
     * @date 2020/12/29 11:14
     */
    public void addTag(Article article) {

        Integer articleId = article.getId();
        List<Tag> tagByArticle = tagMapper.getTagByArticle(articleId);
        article.setTags(tagByArticle);//设置文章的标签集

    }

    /**
     * 获取文章阅读量
     *
     * @param author
     * @return java.lang.Integer
     * @author yeqian
     * @date 2020/12/28 14:04
     */
    @Override
    public Integer getReadCount(Integer author) {
        return articleMapper.getReadCount(author);
    }

    /**
     * 根据文章状态获取文章数量
     *
     * @param status
     * @return java.lang.Integer
     * @author yeqian
     * @date 2020/12/27 23:08
     */
    @Override
    public Integer getTotalCount(Integer status, Integer author) {
        Map map = new HashMap();
        map.put("status", status);
        map.put("author", author);
        return articleMapper.getTotalCount(map);
    }

    /**
     * 根据主键设置回收站
     *
     * @param id
     * @return void
     * @author yeqian
     * @date 2020/12/27 16:47
     */
    @Override
    public void setStatus(Integer id, Integer status) {

        Map map = new HashMap();
        map.put("id", id);
        map.put("status", status);
        articleMapper.setStatus(map);
    }

    /**
     * 根据主键修改文章成功
     *
     * @param article
     * @return void
     * @author yeqian
     * @date 2020/12/25 0:16
     */
    @Override
    public void updateArticle(Article article) {
        // 1 修改文章
        articleMapper.updateByPrimaryKeySelective(article);
        // 2 获取文章id
        Integer articleId = article.getId();
        System.out.println(articleId);
        // 3 删除原有的标签
        articleMapper.deleteArticleId(articleId);
        // 4 修改文章绑定的标签
        if (article.getTags() != null) {
            for (Tag tag : article.getTags()) {
                saveArticleAndTag(tag, articleId);
            }
        }
    }

    /**
     * 根据主键删除文章
     *
     * @param id
     * @return void
     * @author yeqian
     * @date 2020/12/25 0:04
     */
    @Override
    public void deleteArticle(Integer id) {
        // 删除文章
        articleMapper.deleteByPrimaryKey(id);
        // 删除文章的置顶关系
        articleMapper.deleteArticleAndRealaTionShip(id);
    }

    /**
     * 根据主键查询文章
     *
     * @param id
     * @return
     */
    @Override
    public Article getArticle(Integer id) {
        Article article = articleMapper.getArticle(id);
        addTagName(article);
        return article;
    }

    /**
     * 根据作者博客分页查询
     *
     * @param
     * @return com.yeqian.entity.PageResult
     * @author yeqian
     * @date 2020/12/24 23:41
     */
    @Override
    public PageResult listQueryPage(QueryPageBean queryPageBean, Integer status) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        Map map = new HashMap<>();
        map.put("author", queryPageBean.getQueryString());
        map.put("status", status);

        PageHelper.startPage(currentPage, pageSize);
        Page<Article> page = articleMapper.listByCondition(map);
        long total = page.getTotal();

        List<Article> result = page.getResult();
        //设置作者名字
        for (Article article : result) {
            String authorName = userMapper.getArticleName(article.getAuthor());

            article.setAuthorName(authorName);
            addTagName(article);
        }
        return new PageResult(total, result);
    }

    /**
     * 添加文章
     *
     * @param article
     * @return void
     * @author yeqian
     * @date 2020/12/24 23:03
     */
    @Override
    public void saveArticle(Article article) {
        articleMapper.saveArtice(article);
        // logger.error("article" + article);
        Integer articleId = article.getId();
        //设置默认为不置顶
        Map<String, Integer> map = new HashMap<>();
        map.put("articleId", articleId);
        map.put("sort", 0);// 0代表默认不置顶
        saveArticleAndRealaTionShip(map);
        if (article.getTags() != null) {
            for (Tag tag : article.getTags()) {
                saveArticleAndTag(tag, articleId);
            }
        }
    }


    /**
     * 保存文章与标签之前的关系
     *
     * @param tag
     * @param articleId
     * @return void
     * @author yeqian
     * @date 2021/1/1 19:26
     */
    public void saveArticleAndTag(Tag tag, Integer articleId) {
        //1. 根据标签名，查询标签，判断标签是否存在
        String tagName = tag.getName();
        Integer tagId = tagMapper.getTagByName(tagName);

        if (tagId == null) { //如果此标签不存在那么创建此标签然后设置关系
            tagMapper.saveTag(tag);
            tagId = tag.getId();
        }
        //如果存在
        Map map = new HashMap<>();
        //然后查询标签与文章的对应关系，如果已经有了，那么就不会设置此标签的对应关系
        List<Integer> articleIds = tagMapper.getArtileAndTag(tagId);
        if (!articleIds.contains(articleId)) {//如果不包含，证明没有此文章id与此标签id的对应关系
            map.put("articleId", articleId);
            map.put("tagId", tagId);
            articleMapper.saveArticleAndTag(map);

        }
    }
}
